package com.vitabuybuy.tv.ui.mine.comment

import android.graphics.Color
import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.dto.MessageEvent
import com.library.common.net.loadingtype.LoadingType
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityMyCommentBinding
import com.vitabuybuy.tv.dialog.DeletePopup
import com.library.common.toast.XToast
import com.vitabuybuy.tv.ui.mine.comment.adapter.MyCommentListAdapter
import com.vitabuybuy.tv.ui.mine.comment.viewmodel.MyCommentViewModel
import com.vitabuybuy.tv.utils.EventCode
import kotlinx.android.synthetic.main.activity_my_comment.*
import kotlinx.android.synthetic.main.activity_my_comment.refresh

class MyCommentActivity :
    BaseActivity<MyCommentViewModel, ActivityMyCommentBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_my_comment

    private val myCommentListAdapter by lazy { MyCommentListAdapter() }
    private var pageNumber = 1
    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.comment_center))
        mBinding.mViewModel = viewModel
        listview.apply {
            adapter = myCommentListAdapter
            layoutManager = LinearLayoutManager(mContext)
        }
    }

    override fun observeData() {
        viewModel.getCommentList(pageNumber)
        viewModel.datas.observe(this, Observer {
            if (pageNumber == 1) {
                myCommentListAdapter.setNewData(it)
            } else {
                myCommentListAdapter.addData(it)
            }
            if (myCommentListAdapter.mData.isEmpty()) {
                viewModel.statusView.set(LoadingType.SHOW_EMPTY)
            } else {
                viewModel.statusView.set(LoadingType.SHOW_CONTENT)
            }
        })
    }

    override fun eventHandling() {
        refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                super.onLoadMore(refreshLayout)
                pageNumber++
                viewModel.getCommentList(pageNumber)
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                super.onRefresh(refreshLayout)
                pageNumber = 1
                viewModel.getCommentList(pageNumber)
            }
        })
        myCommentListAdapter.onClickMore = { position, view ->
            val deletePopup = DeletePopup(mContext) {
                viewModel.delete(myCommentListAdapter.mData[position].id, position)
            }
            deletePopup.setBackgroundColor(Color.TRANSPARENT)
            deletePopup.showPopupWindow(view)
        }
        myCommentListAdapter.setOnItemClickListener { binding, item, position ->
            CommentDetailActivity.openActivity(mContext, item.id, position)
        }
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            0 -> {
                pageNumber = 1
                viewModel.getCommentList(pageNumber)
            }
            1 -> {
                refresh.onLoad(msg.arg1)
            }
            2 -> {
                XToast.success(getString(R.string.already_delete))
                myCommentListAdapter.removeAt(msg.arg1)
                if (myCommentListAdapter.mData.isEmpty()) {
                    viewModel.statusView.set(LoadingType.SHOW_EMPTY)
                } else {
                    viewModel.statusView.set(LoadingType.SHOW_CONTENT)
                }
            }
        }
    }

    override fun onEvent(event: MessageEvent) {
        when (event.code) {
            EventCode.delete_comment -> {
                myCommentListAdapter.removeAt(event.message as Int)
                if (myCommentListAdapter.mData.isEmpty()) {
                    viewModel.statusView.set(LoadingType.SHOW_EMPTY)
                } else {
                    viewModel.statusView.set(LoadingType.SHOW_CONTENT)
                }
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>) {
            mContext.startActivity(null, MyCommentActivity::class.java)
        }
    }
}
